﻿import com.yourdomain.spreadshop.gui.mod.DevTool;
import com.yourdomain.spreadshop.gui.mod.MouseCursor;
import com.yourdomain.spreadshop.gui.mod.ItemsBoard;
import com.yourdomain.spreadshop.gui.mod.ItemDetail;
import com.yourdomain.spreadshop.gui.mod.ShoppingCart;
import com.yourdomain.spreadshop.gui.mod.TextBox;
import com.yourdomain.spreadshop.gui.mod.InfoBox;

import com.yourdomain.spreadshop.gui.mod.lib.ErrorLog;

import net.spreadshirt.controller.Controller;

import net.spreadshirt.logger.Logger;

/**
 * The GUI (graphical user interface) class represents a specific
 * sub class of the View class.
 *
 * It creates and initializes some view modules to support the user
 * with a showcase of the shops articles and a detail view of every
 * product and a shopping cart that stores all items to order.
 */
class com.yourdomain.spreadshop.gui.GUI extends net.spreadshirt.view.View {

	// The path to the swf folder.
	public var swf_path:String;

	// The movie clip container.
	private var _clip:MovieClip;
	private var _clip_infos:MovieClip;
	private var _clip_cursor:MovieClip;

	// These variables stores the modules.
	private var _devTool:DevTool;
	private var _itemsBoard:ItemsBoard;
	private var _itemDetail:ItemDetail;
	private var _shoppingCart:ShoppingCart;
	private var _textBox:TextBox;
	private var _infoBox:InfoBox;
	private var _mouseCursor:MouseCursor;

	// The job list to initialize the view modules.
	private static var jobList:Array = [
		"initDevTool",
		"initItemsBoard",
		"initItemDetail",
		"initShoppingCart",
		"initTextBox",
		"initInfoBox",
		"initMouseCursor"
	];
	private var _initModulesJobs:Array;

	/**
	 * GUI Constructor
	 */
	public function GUI(clip:MovieClip) {
		// Create various movie clip containers.

		_clip = clip.createEmptyMovieClip("modules", 10);
		_clip_infos = clip.createEmptyMovieClip("infoModules", 20);
		_clip_cursor = clip.createEmptyMovieClip("cursorAddon", 30);
		// Parse path to the swf folder.
		swf_path = (_level0.swf_path == undefined)
			? "./"
			: _level0.swf_path
		;
		// Disable focus rectangle for all movie clips.
		_root._focusrect = false;

		// Setup main stage settings
		_quality = "BEST";
		Stage.align = "TL";
		Stage.scaleMode = "noScale";
		// Broadcasts the resize event to all modules.
		AsBroadcaster.initialize(this);
		var stageListener:Object = new Object();
		stageListener.gui = this;
		stageListener.onResize = function() {
			this.gui.broadcastMessage("checkPosition");
		};
		Stage.addListener(stageListener);
		// Create error log and observer.
		var log:Logger = Logger.getLog();
		var callObj:Object = {
			obj: this,
			fnc: "showError"
		};
		var errorLog:ErrorLog = new ErrorLog(log, callObj);
		log.addObserver(errorLog);
	}

	/**
	 * Declare actionscript broadcaster method.
	 */
	public function addListener(listener:Object):Void {
	}

	/**
	 * Starts initializing the modules.
	 */
	public function init():Void {
		initModules();
	}

	/**
	 * Resets some modules.
	 */
	public function reset():Void {
		_itemDetail.hide();
		_shoppingCart.hide();
		_textBox.hide();
	}

	/**
	 * Initializes the modules.
	 */
	private function initModules():Void {
		// Creates a copy of the job list.
		_initModulesJobs = new Array();
		for(var i:Number = 0; i < jobList.length; i++) {
			_initModulesJobs.push(jobList[i]);
		}
		// Load all data
		Logger.getLog().info("GUI - loading modules");
		// start all Jobs
		for(var n:Number = 0; n < _initModulesJobs.length; n++) {
			this[_initModulesJobs[n]]();
		}
	}

	private function initDevTool():Void {
		var level:Number = _clip.getNextHighestDepth();
		_devTool = new DevTool(this, _clip, level);
	}

	private function initItemsBoard():Void {
		var level:Number = _clip.getNextHighestDepth();
		_itemsBoard = new ItemsBoard(this, _clip, level);
	}

	private function initItemDetail():Void {
		var level:Number = _clip.getNextHighestDepth();
		_itemDetail = new ItemDetail(this, _clip, level);
	}

	private function initShoppingCart():Void {
		var level:Number = _clip.getNextHighestDepth();
		_shoppingCart = new ShoppingCart(this, _clip, level);
	}

	private function initTextBox():Void {
		var level:Number = _clip.getNextHighestDepth();
		_textBox = new TextBox(this, _clip, level);
	}

	private function initInfoBox():Void {
		var level:Number = _clip_infos.getNextHighestDepth();
		_infoBox = new InfoBox(this, _clip_infos, level);
	}

	private function initMouseCursor():Void {
		var level:Number = _clip_cursor.getNextHighestDepth();
		_mouseCursor = new MouseCursor(this, _clip_cursor, level);
	}

	/**
	 * Job is done. If all jobs are done, update view.
	 */
	public function initModuleDone(jobName:String):Void {
		for(var n:Number = 0; n < _initModulesJobs.length; n++) {
			var method:String = _initModulesJobs[n];
			if(jobName == method) {
				_initModulesJobs.splice(n, 1);
			}
			if(_initModulesJobs.length < 1) {
				Logger.getLog().info("GUI -"
					+" loading modules complete");
				onLoadInit();
				setupStyles();
			}
		}
	}

	/**
	 * Updates the items and values in the items board module.
	 */
	private function updateArticles(evt:Object):Void {
		// Setup categories combobox.
		var categoriesList:Array = controller.getCategoryItems();
		_itemsBoard.showCategories(categoriesList);
		// Shows the article thumbnail icons of all categories.
		showItemsByCategory();
	}

	/**
	 * Updates the items and values in the shopping cart module.
	 */
	private function updateBasket(evt:Object):Void {
		var basket:Object = evt.target._basket.getBasket();
		_shoppingCart.showBasket(basket);
	}

	/**
	 * Shows the transmission indicator.
	 */
	private function startLoading(evt:Object):Void {
		_mouseCursor.startTransmissionIndicator();
	}

	/**
	 * Hides the transmission indicator.
	 */
	private function stopLoading(evt:Object):Void {
		_mouseCursor.stopTransmissionIndicator();
	}

	/**
	 * Displays an error message.
	 */
	private function showError(msg:String):Void {
		showInfo(msg);
	}

	/**
	 * Shows the dragging indicator.
	 */
	public function startDragging():Void {
		_mouseCursor.startDraggingIndicator();
	}

	/**
	 * Hides the dragging indicator.
	 */
	public function stopDragging():Void {
		_mouseCursor.stopDraggingIndicator();
	}

	/**
	 * Shows the scrolling indicator.
	 */
	public function startScrolling():Void {
		_mouseCursor.startScrollingIndicator();
	}

	/**
	 * Hides the scrolling indicator.
	 */
	public function stopScrolling():Void {
		_mouseCursor.stopScrollingIndicator();
	}

	/**
	 * Shows the tooltip.
	 */
	public function startTooltip(str:String):Void {
		_mouseCursor.startTooltip(str);
	}

	/**
	 * Hides the tooltip.
	 */
	public function stopTooltip():Void {
		_mouseCursor.stopTooltip();
	}

	/**
	 * Switches the items board.
	 */
	public function showItemsByCategory(category_id:Number):Void {
		if(isNaN(category_id)) {
			category_id = -1;
		}
		// Show the article thumbnail icons of the specified category.
		var items:Array;
		items = controller.getArticleItemsByCategory(category_id); // ### items is blank! ##################################
		trace("items.length: "+items.length);
		for (var i= 0; i< items.length; i++)
		trace("items["+i+"]: "+ items[i]);
		_itemsBoard.showItems(items);
	}

	/**
	 * Shows items details in the item detail view modul.
	 */
	public function showItem(
		article_id:Number,
		producttype_size_id:Number,
		quantity:Number
	):Void {
		var article:Object = controller.getArticleItem(article_id);
		_itemDetail.showItem(article, producttype_size_id, quantity);
	}

	/**
	 * Shows the terms and conditions in the text box.
	 */
	public function showTNC():Void {
		var text:String = controller.getTNC();
		_textBox.showText(text);
	}

	/**
	 * Shows the terms and conditions in the text box.
	 */
	public function showInfo(text:String):Void {
		_infoBox.showInfo(text);
	}

	/**
	 * Returns all design items.
	 *
	 * @return	An array of all design items.
	 */
	public function getDesignItems():Array {
		return controller.getDesignItems();
	}

	/**
	 * Returns a single design item.
	 *
	 * @param	design_id	The design ID.
	 *
	 * @return	A single design item.
	 */
	public function getDesignItem(design_id:Number):Object {
		return controller.getDesignItem(design_id);
	}

	/**
	 * Setup some default component style settings.
	 */
	public function setupStyles():Void {
		with(_global.styles) {
			setStyle("themeColor", "haloBlue");
			setStyle("color", "0x0099ff");
			setStyle("fontFamily", "Tahoma");
			setStyle("fontSize", "12");
			setStyle("embedFonts", true);
		}
		with(_global.styles.TextArea) {
			setStyle("fontFamily", "Courier New");
			setStyle("fontSize", "13");
			setStyle("embedFonts", false);
			setStyle("fontStyle", "normal");
			setStyle("fontWeight", "none");
			setStyle("textAlign", "left");
		}
		with(_global.styles.ComboBox) {
			setStyle("borderStyle", "none");
			setStyle("openDuration", "150");
			setStyle("marginLeft", "8");
		}
	}

}
